import { createRouter, createWebHistory } from "vue-router";


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:"/login",
      name:"login",
      component:()=>import("@/login/mylogin.vue")
    },
    {
      //路由重定向
      path:"/",
      redirect:"/home",
    },
    {
      path:"/home",
      name:"home",
      component:()=>import("@/home/myhome.vue")
    },
    {
      path:"/search",
      name:"search",
      component:()=>import("@/search/mysearch.vue")
    },
    {
      path:"/hotel",
      name:"hotel",
      component:()=>import("@/guide/hotel/myhotel.vue"),
    },
    {
      path:"/address",
      name:"address",
      component:()=>import("@/guide/hotel/address.vue")
    },
    {
      path:"/hoteldetail",
      name:"hoteldetail",
      component:()=>import("@/detail/myhoteldetail.vue")
    },
    {
      path:"/buy",
      name:"buy",
      component:()=>import("@/buybuynuy/mybuy.vue"),
      meta:{
        //需要token状态
        needlogin:true
      }
    }
  ],
});

router.beforeEach((to,from,next)=>{
  //判断是否需要权限
  if(to.meta.needlogin){
    //判断是否有token
    if(localStorage.getItem("token")){
      //有就放行
      next()
    }else{
      //否则打到登录页
      next("/login")
    }
  }else{
    //不需要校验直接放行
    next()
  }
})

export default router;
